import router from './router'
import store from './store'
import {
	Message
} from 'element-ui'
import {
	getToken
} from '@/utils/auth'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 显示进度条
NProgress.configure({
	showSpinner: false
})

const whiteList = ['/login']

// 前置钩子：处理未登录无法访问登录以外的网页
router.beforeEach((to, _from, next) => {
	// 开启进度条
	NProgress.start()

	// 判断是否登录
	if (getToken()) {
		if (to.path === '/login') {
			next('/dashboard')
			NProgress.done()
		} else {
			store.dispatch('GetMemberInfo').then(() => {
				next()
			}).catch(error => {
				store.dispatch('LogOut').then(() => {
					Message({
						message: error,
						type: 'error',
						duration: 3 * 1000
					})
					next('/login')
				})
			})
			NProgress.done()
		}

	} else {
		if (whiteList.indexOf(to.path) !== -1) {
			next()
			NProgress.done()
		}
		else {
			next('/login')
			NProgress.done()
		}
	}

})

